


import { registerClass } from "../../../cfw/tools/class_decorator";
import BaseAd from "../../core/base/BaseAd";
import { ResultState, SDKState } from "../../SDKDefine";

@registerClass('GoogleRewardAd')
export default class GoogleRewardAd extends BaseAd {
    hide(): void {
    }
    load(): void {
        if (this.ad) {
            this.ad.load();
        }
    }

    show() {
        if (this.ad) {
            this.ad.show().then(() => {
            }).catch(() => {
                this.ad.load()
                    .then(() => {
                        this.ad.show()
                    })
                    .catch((err: any) => {
                    })
            })
        }
    }
    onClose(res: any) {
        console.log('NativeTest onClose ', res)
        if (res && res.isEnded || res === undefined) {
            this.callResultCallback(ResultState.YES)
        } else {
            this.callResultCallback(ResultState.NO)
        }
    }
    create() {
        if (!this.ad) {
            this.ad = this.sdk.createRewardedVideoAd({
                adUnitId: this.adUnitID
            })
            this.ad.onLoad(this.getFunc(this.onLoad))
            this.ad.onError(this.getFunc(this.onError))
            this.ad.onClose(this.getFunc(this.onClose))
        }
    }
    onLoad() {
        this.setState(SDKState.loadSucess)
    }
    destroy(): void {
        if (this.ad) {
            this.ad.offLoad(this.getFunc(this.onLoad))
            this.ad.offError(this.getFunc(this.onError))
            this.ad.offClose(this.getFunc(this.onClose))
            this.ad.destroy();
            this.ad = null;
        }
    }

    onError(err: any) {
        this.setState(SDKState.loadFail)
    }

}